//
// Created by v on 19-7-8.
//

#ifndef CUTEDEVICE_C600_CMLOCK_H
#define CUTEDEVICE_C600_CMLOCK_H

#include <pthread.h>

#include <iostream>

typedef void (*cm_lock_logprint)(int level, const char *data);

class CMLock
{
public:
    explicit CMLock(int printInterval = 15);
    virtual ~CMLock();

    void setLogPrint(cm_lock_logprint logprint)
    {
        m_logprint = logprint;
    }

    int lock(const char *func, int line);
    int trylock();
    int unlock();

    pthread_mutex_t *getMutex()
    {
        return &m_mutex;
    }

private:
    pthread_mutex_t m_mutex;
    std::string m_lastFunc;
    int m_lastLine;

    int m_lastPrintTime;
    int m_printInterval;

    cm_lock_logprint m_logprint;
};

#endif  // CUTEDEVICE_C600_CMLOCK_H
